﻿(function () {

    $(document).ready(function () {

        $('.pageLink').on('click', function (e) {
            e.preventDefault();
            var $this = $(this);
            var pageObject = { page: $this.attr("data-page") };

            $.ajax({
                type: "POST",
                url: $('#RequestPagedPetURL').val(),
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(pageObject),
                success: function (result) {

                    var $petsContainer = $('.petsContainer');
                    $petsContainer.empty();

                    for (var i = 0; i < result.length; i++) {
                        var template = $('.adoptEmptyTemplate').clone();

                        result[i].ThumbPic = result[i].HasPicture ? $("#ImageThumbServiceURL").val() + "&pictureId=" + result[i].PetId : "";
                        result[i].HasPicture = result[i].HasPicture ? "inline" : "none";
                        result[i].BirthDate = new Date(parseInt(result[i].BirthDate.substr(6)));

                        $petsContainer.append($.tmpl(template, result[i]));
                    }

                },
                error: function (xhr, status, msg) {
                }
            });
        });
    });

})()